<?php

class Eleven_Model_Student extends CMS_Model_Acl_Abstract
{
	public function getCurrentTopic($topicId)
	{
		return $this->getResource('studentTopic')->getCurrentTopic($topicId);
	}
	
	public function isCurrentTopic($topicId)
	{
		$currentTopic = $this->getCurrentTopic();
		$searchTopic = $this->getCurrentTopic($topicId); 
		return $currentTopic->id == $searchTopic->id;
	}
		
    public function getTopics($paged = false, $order = null)
	{
	    return $this->getResource('studentTopic')->getTopics($paged, $order);
	}
	
	public function deleteTopicById($id)
	{
		$contents = $this->getResource('studentContent')->getContentsByTopicId($id);
		return $this->getResource('studentTopic')->deleteTopicById($id, $contents);
	}
	
	public function getCurrentContents($topicId = false, $paged = false, $type = null)
	{
	    if (!$topicId)
	    {
	        $topicId = $this->getCurrentTopic()->id;
	    }
	    
	    return $this->getResource('studentContent')->getContentsByTopicId($topicId, $paged,$type);
	}
	
	public function getContentById($id)
	{
	    $id = (int) $id;
        
        return $this->getResource('studentContent')->getContentById($id);
	}
	
	public function getContentsByTopicId($topicId, $paged = false, $order='updatetime DESC')
	{
		return $this->getResource('studentContent')->getContentsByTopicId($topicId, $paged, NULL, $order);
	}
	
	public function getReplysByContentId($id, $paged, $order)
	{
	    $id = (int) $id;
	    
	    return $this->getResource('studentReply')->getReplysByContentId($id, $paged, $order);
	}
	
    public function saveReply($post)
	{
		$form = $this->getForm('studentReply');
       	return $this->_saveReply($form, $post);
	}
	
    protected function _saveReply($form, $info, $defaults=array())
    {
        if (!$form->isValid($info)) {
            return false;
        }

        // get filtered values
        $data = $form->getValues();

        return $this->getResource('studentReply')->saveRow($data);
    }
	
	public function savePost($post)
	{
	    $form = $this->getForm('studentPublish');
       	return $this->_savePost($form, $post);
	}
	
	protected function _savePost($form, $info, $defaults=array())
    {
        if (!$form->isValid($info)) {
            return false;
        }
        
        $fullFilePath = $form->file->getFileName();

        // get filtered values
        $data = $form->getValues();

        $topic = $this->getResource('studentTopic')->getCurrentTopic();
        $data['topicid'] = $topic->id;
        if($data['file'])  $data['filepath'] = $this->_rename($fullFilePath);
        $data['updatetime'] = time();
        
        return $this->getResource('studentContent')->saveRow($data);
    }
    
    private function _rename($fullFilePath)
    {
        $locationFile = $fullFilePath;
        $ext=end(explode(".",$locationFile));
        $currentMicroTime = Internim_Date::getMicrotime();
        $nameFile = $currentMicroTime.'.'.$ext;
        $fullPathNameFile = './data/uploads/student/'.$nameFile;
        
        $filterRename = new Zend_Filter_File_Rename(array('target'=> $fullPathNameFile, 'overwrite' => true));
        $filterRename->filter(iconv('UTF-8', 'GB2312', $locationFile));
        
        $target = $filterRename->getFile();
        
        return $target[0]['target'];
    }
    
	public function saveContentEdit($post)
	{
		$form = $this->getForm('studentContent');
       	return $this->_saveContent($form, $post);
	}
	
	protected function _saveContent($form, $info)
	{
		if (!$form->isValid($info)) {
            return false;
        }

		$data = $form->getValues();
		
		$content = $data['id'] != '' ?  $this->getResource('studentContent')->getContentById($data['id']) : null;
            		        
        return $this->getResource('studentContent')->saveRow($data, $content);
	}
	
	public function deleteContentById($contentId)
	{
		return $this->getResource('studentContent')->deleteContentById($contentId);
	}
	
    public function saveTopicEdit($post)
	{
	    $form = $this->getForm('studentTopic');
       	return $this->_saveTopic($form, $post);
	}
	
    protected function _saveTopic($form, $info, $defaults=array())
	{
		if (!$form->isValid($info)) {
            return false;
        }

		$data = $form->getValues();
		
		$topic = $data['id'] != '' ?  $this->getResource('studentTopic')->getCurrentTopic($data['id']) : null;
            		        
        return $this->getResource('studentTopic')->saveRow($data, $topic);
	}
	
    public function setTops($tops, $isCancel)
    {
        return $this->getResource('studentContent')->setTops($tops, $isCancel);
    }
    
/**
     * @param CMS_Acl_Interface CMS_Acl_Interface CMS_Acl_Interface $acl
     */
    public function setAcl (CMS_Acl_Interface $acl)
    {
        if (!$acl->has($this->getResourceId()))
        {
            $acl->add($this)
                ->allow('Guest', $this, array('index','view','savereply','listtopic',))
                ->allow('Student', $this, array('publish','save'))
                ->allow('Teacher');
                
            $this->_acl = $acl;
            return $this;
        }
    }

/**
     * 
     */
    public function getAcl ()
    {
        if (null === $this->_acl)
        {
            $this->setAcl(new Eleven_Model_Acl());
        }
        
        return $this->_acl; 
    }

/**
     * 
     */
    public function getResourceId ()
    {
        return 'Student';
    }


}

?>